package cn.tede.file;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/*本类用来测试字节输出流*/
public class TestOut {
    public static void main(String[] args) {
        method();
        method1();
    }

    private static void method1() {
        OutputStream outputStream = null;
        try {
            String a = "JHABW父类hAQluifh";
            byte[] bytes = a.getBytes();
            //创建缓冲字符输出流
            outputStream = new BufferedOutputStream(new FileOutputStream("G:\\IO\\2.txt"));
            //输出字符
            outputStream.write(bytes);
            System.out.println("数据输出成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (outputStream != null) {
                try {
                    //关流操作
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private static void method() {
        OutputStream outputStream = null;
        try {
            //创建字符输出流对象
            //第一种
          //  outputStream = new FileOutputStream("G:\\IO\\3.txt");
            //第二种
            outputStream = new FileOutputStream("G:\\IO\\3.txt",true);
            //对象输出方法
            outputStream.write(100);
            outputStream.write(109);
            System.out.println("数据写出成功");
        } catch (Exception e) {
            System.out.println("写出异常");
        } finally {
            if (outputStream != null) {
                try {//关闭流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
